﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;


namespace Nivdal
{
    public partial class TranslateNewMatchSentences : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            if (ou.UserAccessLevel == "1" || ou.UserAccessLevel == "0")
            {
                Response.Redirect("~/GeneralMessage.aspx?MessageString=" + ou.UserAccessLevel);
            }

            if (!IsPostBack)
            {
                LanguageHiddenField.Value = GND.ReadNivdalIntData("UserId", "Nivdal_GetUserTranslateLanguage", ou.UserId);
                
                if ((LanguageHiddenField.Value == null) || (LanguageHiddenField.Value == "0"))
                    Response.Redirect("GeneralMessage.aspx?MessageString=k882m299nnqmhtsd");
                else if (LanguageHiddenField.Value == "2")
                    Response.Redirect("TranslateEnglish.aspx");

                TranslationStatusDataSourse.SelectParameters.Add("UserId", ou.UserId);
                
                LoadSentence();
            }
        }

        protected void SubmitTranslation(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string PartId, Separator, Text;
            string Answer = "";


            foreach (RepeaterItem item in SentencePartsRepeater.Items)
            {
                PartId = (item.FindControl("PartIdHiddenField") as HiddenField).Value;
                Separator = (item.FindControl("SeparatorsDDL") as DropDownList).SelectedValue;
                Text = (item.FindControl("TranslateTextBox") as TextBox).Text.Trim();

                Answer += GND.ReadNivdalStringData("PartId", "LanguageId", "Separator", "Text", "Nivdal_TranslateNewMatchSentencePart",
                                          PartId, LanguageHiddenField.Value, Separator, Text);
            }
            MessageLabel.Text = Answer;
            LoadSentence();
        }

        private void LoadSentence()
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            SentenceIdHiddenField.Value = GND.ReadNivdalIntData("UserId", "Nivdal_GetNextNewMatchSentenceToTranslate", ou.UserId);

            if (SentenceIdHiddenField.Value == "0")
            {
                TranslatePanel.Visible = false;
            }
            else
            {
                SentenceDescriptionLabel.Text = GND.ReadNivdalStringData("SentenceId", "Nivdal_GetNewMatchSentenceDescription", SentenceIdHiddenField.Value);
                SentencePartsDataSource.SelectParameters.Clear();
                SentencePartsDataSource.SelectParameters.Add("SentenceId", SentenceIdHiddenField.Value);
                SentencePartsRepeater.DataBind();
                StatusForumView.DataBind();
            }            
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected string TranslationLanguageString(object LanguageName)
        {
            return (Resources.Resource.Translate_YouTranslateToLabelText + " " + LanguageName.ToString());
        }

        protected void IgnoreSentence(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            string Answer = GND.ReadNivdalStringData("UserId", "SentenceId", "Nivdal_IgnoreNewMatchSentenceTranslation", ou.UserId, SentenceIdHiddenField.Value);
            MessageLabel.Text = Answer;
            LoadSentence();
        }
    }
}